<?php
/******************************
 * EQdkp
 * Copyright 2002-2003
 * Licensed under the GNU GPL.  See COPYING for full terms.
 * ------------------
 * config.php
 * Began: Sat December 28 2002
 *
 * $Id: config.php 541 2008-05-20 06:56:16Z rspeicher $
 *
 ******************************/

define('EQDKP_INC', true);
define('IN_ADMIN', true);
$eqdkp_root_path = './../';
include_once($eqdkp_root_path . 'common.php');

$fv = new Form_Validate;

// Start session management
$user->start();
$user->setup();
$user->check_auth('a_config_man');

$submit = ( isset($_POST['submit']) ) ? true : false;

if ( $submit )
{
    $action = 'configure';

    $fv->is_number(array(
        'default_alimit' => $user->lang['fv_number'],
        'default_elimit' => $user->lang['fv_number'],
        'default_ilimit' => $user->lang['fv_number'],
        'default_nlimit' => $user->lang['fv_number'],
        'default_rlimit' => $user->lang['fv_number'],
        'active_point_adj' => $user->lang['fv_number'],
        'inactive_point_adj' => $user->lang['fv_number'])
    );
    
    $fv->is_within_range('default_alimit', 1, 9999);
    $fv->is_within_range('default_elimit', 1, 9999);
    $fv->is_within_range('default_ilimit', 1, 9999);
    $fv->is_within_range('default_nlimit', 1, 9999);
    $fv->is_within_range('default_rlimit', 1, 9999);
    
    if ( $fv->is_error() )
    {
        $action = 'display';
    }
}
else
{
    $action = 'display';
}

//
// Processing
//
switch ( $action )
{
    case 'configure':
        $_POST = htmlspecialchars_array($_POST);
        
        // Update each setting one-by-one
        update_config('guildtag', $_POST['guildtag']);
        update_config('server_name', $_POST['server_name']);
        update_config('server_port', $_POST['server_port']);
        update_config('server_path', $_POST['server_path']);
        update_config('main_title', $_POST['main_title']);
        update_config('sub_title', $_POST['sub_title']);
        update_config('dkp_name', $_POST['dkp_name']);
        update_config('account_activation', ( isset($_POST['account_activation']) ) ? $_POST['account_activation'] : '0');
        update_config('default_alimit', $_POST['default_alimit']);
        update_config('default_elimit', $_POST['default_elimit']);
        update_config('default_ilimit', $_POST['default_ilimit']);
        update_config('default_nlimit', $_POST['default_nlimit']);
        update_config('default_rlimit', $_POST['default_rlimit']);
        update_config('default_lang', $_POST['default_lang']);
        update_config('default_style', $_POST['default_style']);
        update_config('hide_inactive', ( isset($_POST['hide_inactive']) ) ? $_POST['hide_inactive'] : '0');
        update_config('inactive_period', $_POST['inactive_period']);
        update_config('active_point_adj', $_POST['active_point_adj']);
        update_config('inactive_point_adj', $_POST['inactive_point_adj']);
        update_config('enable_gzip', ( isset($_POST['enable_gzip']) ) ? $_POST['enable_gzip'] : '0');
        update_config('show_item_stats', ( isset($_POST['show_item_stats']) ) ? $_POST['show_item_stats'] : '0');
        update_config('cookie_domain', $_POST['cookie_domain']);
        update_config('cookie_name', $_POST['cookie_name']);
        update_config('cookie_path', $_POST['cookie_path']);
        update_config('session_length', $_POST['session_length']);
        update_config('admin_email', $_POST['admin_email']);
        
        // Permissions
        $sql = 'SELECT auth_id, auth_value
                FROM ' . AUTH_OPTIONS_TABLE . '
                ORDER BY auth_id';
        $result = $db->query($sql);
        while ( $row = $db->fetch_record($result) )
        {
            update_auth_default($row['auth_value'], ( isset($_POST[$row['auth_value']]) ) ? 'Y' : 'N');
        }

        header('Location: config.php'.$SID);

        break;
    case 'display':
        // Find default auth settings
        $sql = 'SELECT auth_id, auth_default
                FROM ' . AUTH_OPTIONS_TABLE . '
                ORDER BY auth_id';
        $result = $db->query($sql);
        while ( $row = $db->fetch_record($result) )
        {
            $auth_defaults[$row['auth_id']] = $row['auth_default'];
        }
        $db->free_result($result);
        
        $tpl->assign_vars(array(
            'F_CONFIG' => 'config.php'.$SID,

            'L_GENERAL_SETTINGS' => $user->lang['general_settings'],
            'L_GUILDTAG' => $user->lang['guildtag'],
            'L_GUILDTAG_NOTE' => $user->lang['guildtag_note'],
            'L_DOMAIN_NAME' => $user->lang['domain_name'],
            'L_SERVER_PORT' => $user->lang['server_port'],
            'L_SERVER_PORT_NOTE' => $user->lang['server_port_note'],
            'L_SCRIPT_PATH' => $user->lang['script_path'],
            'L_SCRIPT_PATH_NOTE' => $user->lang['script_path_note'],
            'L_SITE_NAME' => $user->lang['site_name'],
            'L_SITE_DESCRIPTION' => $user->lang['site_description'],
            'L_POINT_NAME' => $user->lang['point_name'],
            'L_POINT_NAME_NOTE' => $user->lang['point_name_note'],
            'L_ENABLE_ACCOUNT_ACTIVATION' => $user->lang['enable_account_activation'],
            'L_NONE' => $user->lang['none'],
            'L_USER' => $user->lang['user'],
            'L_ADMIN' => $user->lang['admin'],
            'L_ADJUSTMENTS_PER_PAGE' => $user->lang['adjustments_per_page'],
            'L_EVENTS_PER_PAGE' => $user->lang['events_per_page'],
            'L_ITEMS_PER_PAGE' => $user->lang['items_per_page'],
            'L_NEWS_PER_PAGE' => $user->lang['news_per_page'],
            'L_RAIDS_PER_PAGE' => $user->lang['raids_per_page'],
            'L_DEFAULT_LANGUAGE' => $user->lang['default_language'],
            'L_DEFAULT_STYLE' => $user->lang['default_style'],
            'L_PREVIEW' => $user->lang['preview'],
            'L_HIDE_INACTIVE' => $user->lang['hide_inactive'],
            'L_HIDE_INACTIVE_NOTE' => $user->lang['hide_inactive_note'],
            'L_INACTIVE_PERIOD' => $user->lang['inactive_period'],
            'L_INACTIVE_PERIOD_NOTE' => $user->lang['inactive_period_note'],
            'L_ACTIVE_POINT_ADJ' => $user->lang['active_point_adj'],
            'L_ACTIVE_POINT_ADJ_NOTE' => $user->lang['active_point_adj_note'],
            'L_INACTIVE_POINT_ADJ' => $user->lang['inactive_point_adj'],
            'L_INACTIVE_POINT_ADJ_NOTE' => $user->lang['inactive_point_adj_note'],
            'L_ENABLE_GZIP' => $user->lang['enable_gzip'],
            'L_YES' => $user->lang['yes'],
            'L_NO' => $user->lang['no'],
            'L_DEFAULT_PERMISSIONS' => $user->lang['default_permissions'],
            'L_DEFAULT_PERMISSIONS_NOTE' => $user->lang['default_permissions_note'],
            'L_LIST' => $user->lang['list'],
            'L_VIEW' => $user->lang['view'],
            'L_ADD' => $user->lang['add'],
            'L_UPDATE' => $user->lang['update'],
            'L_DELETE' => $user->lang['delete'],
            'L_MANAGE' => $user->lang['manage'],
            'L_CONFIGURATION' => $user->lang['configuration'],
            'L_PLUGINS' => $user->lang['plugins'],
            'L_STYLES' => $user->lang['styles'],
            'L_USERS' => $user->lang['users'],
            'L_MEMBERS' => $user->lang['members'],
            'L_LOGS' => $user->lang['logs'],
            'L_VIEW' => $user->lang['view'],
            'L_EVENTS' => $user->lang['events'],
            'L_GROUP_ADJUSTMENTS' => $user->lang['group_adjustments'],
            'L_INDIVIDUAL_ADJUSTMENTS' => $user->lang['individual_adjustments'],
            'L_ITEMS' => $user->lang['items'],
            'L_NEWS' => $user->lang['news'],
            'L_RAIDS' => $user->lang['raids'],
            'L_TURN_INS' => $user->lang['turn_ins'],
            'L_COOKIE_SETTINGS' => $user->lang['cookie_settings'],
            'L_COOKIE_DOMAIN' => $user->lang['cookie_domain'],
            'L_COOKIE_NAME' => $user->lang['cookie_name'],
            'L_COOKIE_PATH' => $user->lang['cookie_path'],
            'L_SESSION_LENGTH' => $user->lang['session_length'],
            'L_EMAIL_SETTINGS' => $user->lang['email_settings'],
            'L_ADMIN_EMAIL' => $user->lang['admin_email'],
            'L_SUBMIT' => $user->lang['submit'],
            'L_RESET' => $user->lang['reset'],

            'GUILDTAG' => $eqdkp->config['guildtag'],
            'SERVER_NAME' => $eqdkp->config['server_name'],
            'SERVER_PORT' => $eqdkp->config['server_port'],
            'SERVER_PATH' => $eqdkp->config['server_path'],
            'MAIN_TITLE' => $eqdkp->config['main_title'],
            'SUB_TITLE' => $eqdkp->config['sub_title'],
            'DKP_NAME' => $eqdkp->config['dkp_name'],
            'ACTIVATION_NONE_CHECKED' => ( $eqdkp->config['account_activation'] == USER_ACTIVATION_NONE ) ? ' checked="checked"' : '',
            'ACTIVATION_USER_CHECKED' => ( $eqdkp->config['account_activation'] == USER_ACTIVATION_SELF ) ? ' checked="checked"' : '',
            'ACTIVATION_ADMIN_CHECKED' => ( $eqdkp->config['account_activation'] == USER_ACTIVATION_ADMIN ) ? ' checked="checked"' : '',
            'DEFAULT_ALIMIT' => $eqdkp->config['default_alimit'],
            'DEFAULT_ELIMIT' => $eqdkp->config['default_elimit'],
            'DEFAULT_ILIMIT' => $eqdkp->config['default_ilimit'],
            'DEFAULT_NLIMIT' => $eqdkp->config['default_nlimit'],
            'DEFAULT_RLIMIT' => $eqdkp->config['default_rlimit'],
            'HIDE_INACTIVE_YES_CHECKED' => ( $eqdkp->config['hide_inactive'] == '1' ) ? ' checked="checked"' : '',
            'HIDE_INACTIVE_NO_CHECKED' => ( $eqdkp->config['hide_inactive'] == '0' ) ? ' checked="checked"' : '',
            'INACTIVE_PERIOD' => $eqdkp->config['inactive_period'],
            'ACTIVE_POINT_ADJ' => $eqdkp->config['active_point_adj'],
            'INACTIVE_POINT_ADJ' => $eqdkp->config['inactive_point_adj'],
            'GZIP_YES_CHECKED' => ( $eqdkp->config['enable_gzip'] == '1' ) ? ' checked="checked"' : '',
            'GZIP_NO_CHECKED' => ( $eqdkp->config['enable_gzip'] == '0' ) ? ' checked="checked"' : '',
            
            'EVENT_ADD_CHECKED' => ( $auth_defaults[1] == 'Y' ) ? ' checked="checked"' : '',
            'EVENT_UPD_CHECKED' => ( $auth_defaults[2] == 'Y' ) ? ' checked="checked"' : '',
            'EVENT_DEL_CHECKED' => ( $auth_defaults[3] == 'Y' ) ? ' checked="checked"' : '',
            'EVENT_LIST_CHECKED' => ( $auth_defaults[24] == 'Y' ) ? ' checked="checked"' : '',
            'EVENT_VIEW_CHECKED' => ( $auth_defaults[25] == 'Y' ) ? ' checked="checked"' : '',
            'GROUPADJ_ADD_CHECKED' => ( $auth_defaults[4] == 'Y' ) ? ' checked="checked"' : '',
            'GROUPADJ_UPD_CHECKED' => ( $auth_defaults[5] == 'Y' ) ? ' checked="checked"' : '',
            'GROUPADJ_DEL_CHECKED' => ( $auth_defaults[6] == 'Y' ) ? ' checked="checked"' : '',
            'INDIVADJ_ADD_CHECKED' => ( $auth_defaults[7] == 'Y' ) ? ' checked="checked"' : '',
            'INDIVADJ_UPD_CHECKED' => ( $auth_defaults[8] == 'Y' ) ? ' checked="checked"' : '',
            'INDIVADJ_DEL_CHECKED' => ( $auth_defaults[9] == 'Y' ) ? ' checked="checked"' : '',
            'ITEM_ADD_CHECKED' => ( $auth_defaults[10] == 'Y' ) ? ' checked="checked"' : '',
            'ITEM_UPD_CHECKED' => ( $auth_defaults[11] == 'Y' ) ? ' checked="checked"' : '',
            'ITEM_DEL_CHECKED' => ( $auth_defaults[12] == 'Y' ) ? ' checked="checked"' : '',
            'ITEM_LIST_CHECKED' => ( $auth_defaults[26] == 'Y' ) ? ' checked="checked"' : '',
            'ITEM_VIEW_CHECKED' => ( $auth_defaults[27] == 'Y' ) ? ' checked="checked"' : '',
            'NEWS_ADD_CHECKED' => ( $auth_defaults[13] == 'Y' ) ? ' checked="checked"' : '',
            'NEWS_UPD_CHECKED' => ( $auth_defaults[14] == 'Y' ) ? ' checked="checked"' : '',
            'NEWS_DEL_CHECKED' => ( $auth_defaults[15] == 'Y' ) ? ' checked="checked"' : '',
            'RAID_ADD_CHECKED' => ( $auth_defaults[16] == 'Y' ) ? ' checked="checked"' : '',
            'RAID_UPD_CHECKED' => ( $auth_defaults[17] == 'Y' ) ? ' checked="checked"' : '',
            'RAID_DEL_CHECKED' => ( $auth_defaults[18] == 'Y' ) ? ' checked="checked"' : '',
            'RAID_LIST_CHECKED' => ( $auth_defaults[30] == 'Y' ) ? ' checked="checked"' : '',
            'RAID_VIEW_CHECKED' => ( $auth_defaults[31] == 'Y' ) ? ' checked="checked"' : '',
            'TURNIN_ADD_CHECKED' => ( $auth_defaults[19] == 'Y' ) ? ' checked="checked"' : '',
            'MEMBERS_MAN_CHECKED' => ( $auth_defaults[21] == 'Y' ) ? ' checked="checked"' : '',
            'MEMBER_LIST_CHECKED' => ( $auth_defaults[28] == 'Y' ) ? ' checked="checked"' : '',
            'MEMBER_VIEW_CHECKED' => ( $auth_defaults[29] == 'Y' ) ? ' checked="checked"' : '',
            'CONFIG_MAN_CHECKED' => ( $auth_defaults[20] == 'Y' ) ? ' checked="checked"' : '',
            'PLUGINS_MAN_CHECKED' => ( $auth_defaults[32] == 'Y' ) ? ' checked="checked"' : '',
            'STYLES_MAN_CHECKED' => ( $auth_defaults[33] == 'Y' ) ? ' checked="checked"' : '',
            'USERS_MAN_CHECKED' => ( $auth_defaults[22] == 'Y' ) ? ' checked="checked"' : '',
            'LOGS_VIEW_CHECKED' => ( $auth_defaults[23] == 'Y' ) ? ' checked="checked"' : '',
            
            'COOKIE_DOMAIN' => $eqdkp->config['cookie_domain'],
            'COOKIE_NAME' => $eqdkp->config['cookie_name'],
            'COOKIE_PATH' => $eqdkp->config['cookie_path'],
            'SESSION_LENGTH' => $eqdkp->config['session_length'],
            'ADMIN_EMAIL' => $eqdkp->config['admin_email'])
        );
        
        // Plugin permission defaults
        $plugin_permissions = '';
        do_hook('config_permissions', $plugin_permissions);
        $tpl->assign_vars(array(
            'PLUGIN_PERMISSIONS' => trim($plugin_permissions))
        );

        if ( $dir = @opendir($eqdkp_root_path . 'language/') )
        {
            while ( $file = @readdir($dir) )
            {
                if ( (!is_file($eqdkp_root_path . 'language/' . $file)) && (!is_link($eqdkp_root_path . 'language/' . $file)) && ($file != '.') && ($file != '..') && ($file != 'CVS') )
                {
                    $tpl->assign_block_vars('lang_row', array(
                        'VALUE' => $file,
                        'SELECTED' => ( $eqdkp->config['default_lang'] == $file ) ? ' selected="selected"' : '',
                        'OPTION' => ucfirst($file))
                    );
                }
            }
        }

        $sql = 'SELECT style_id, style_name
                FROM ' . STYLES_TABLE . '
                ORDER BY style_name';
        $result = $db->query($sql);
        while ( $row = $db->fetch_record($result) )
        {
            $tpl->assign_block_vars('style_row', array(
                'VALUE' => $row['style_id'],
                'SELECTED' => ( $eqdkp->config['default_style'] == $row['style_id'] ) ? ' selected="selected"' : '',
                'OPTION' => $row['style_name'])
            );
        }
        $db->free_result($result);

        $page_title = sprintf($user->lang['admin_title_prefix'], $eqdkp->config['guildtag'], $eqdkp->config['dkp_name']).': '.$user->lang['config_title'];
        include_once($eqdkp_root_path . 'includes/page_header.php');

        $tpl->set_filenames(array(
            'body' => 'admin/config.html')
        );
        
        include_once($eqdkp_root_path . 'includes/page_tail.php');
        break;
}

/**
* Updates a config table setting to a new value
*
* @param $config_name
* @param $config_value
* @return bool
*/
function update_config($config_name, $config_value='')
{
    global $db;

    if ( (empty($config_value)) && ($config_value != '0') )
    {
        return false;
    }

    $sql = 'UPDATE ' . CONFIG_TABLE . "
            SET config_value='".strip_tags(htmlspecialchars($config_value))."'
            WHERE config_name='".$config_name."'";
    if ( !($result = $db->query($sql)) )
    {
        return false;
    }
    return true;
}

/**
* Update auth_options default values
* 
* @param $auth_value
* @param $auth_default
* @return bool
*/
function update_auth_default($auth_value, $auth_default='N')
{
    global $db;
    
    if ( empty($auth_value) )
    {
        return false;
    }
    
    $sql = 'UPDATE ' . AUTH_OPTIONS_TABLE . "
            SET auth_default='".strip_tags(htmlspecialchars($auth_default))."'
            WHERE auth_value='".$auth_value."'";
    if ( !($result = $db->query($sql)) )
    {
        return false;
    }
    return true;
}
?>